home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 API Bible / Windows 95 API Bible 3 Disc Set.iso / Win32 API Bible Book 2 of 3 / CHAPTER3 / HITTEST.C < prev    next >
C/C++ Source or Header  |  1996-03-06  |  8KB  |  273 lines

  1.  
  2. #include <windows.h>  
  3. #include <commctrl.h>
  4. #include "hittest.h"  
  5.  
  6.  
  7.  
  8. #if defined (WIN32)
  9.     #define IS_WIN32 TRUE
  10. #else
  11.     #define IS_WIN32 FALSE
  12. #endif
  13.  
  14. #define IS_NT      IS_WIN32 && (BOOL)(GetVersion() < 0x80000000)
  15. #define IS_WIN32S  IS_WIN32 && (BOOL)(!(IS_NT) && (LOBYTE(LOWORD(GetVersion()))<4))
  16. #define IS_WIN95   (BOOL)(!(IS_NT) && !(IS_WIN32S)) && IS_WIN32
  17.  
  18. HINSTANCE hInst;   // current instance
  19.  
  20. LPCTSTR lpszAppName = "MyApp";
  21. LPCTSTR lpszTitle   = "TTM_HITTEST"; 
  22.  
  23.  
  24. BOOL RegisterWin95( CONST WNDCLASS* lpwc );
  25.  
  26.  
  27. int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
  28.                       LPTSTR lpCmdLine, int nCmdShow)
  29. {
  30.    MSG      msg;
  31.    HWND     hWnd; 
  32.    WNDCLASS wc;
  33.  
  34.    wc.style         = CS_HREDRAW | CS_VREDRAW;
  35.    wc.lpfnWndProc   = (WNDPROC)WndProc;       
  36.    wc.cbClsExtra    = 0;                      
  37.    wc.cbWndExtra    = 0;                      
  38.    wc.hInstance     = hInstance;              
  39.    wc.hIcon         = LoadIcon (hInstance, lpszAppName); 
  40.    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  41.    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  42.    wc.lpszMenuName  = lpszAppName;              
  43.    wc.lpszClassName = lpszAppName;              
  44.  
  45.    if ( IS_WIN95 )
  46.    {
  47.       if ( !RegisterWin95( &wc ) )
  48.          return( FALSE );
  49.    }
  50.    else if ( !RegisterClass( &wc ) )
  51.       return( FALSE );
  52.  
  53.    hInst = hInstance; 
  54.  
  55.    hWnd = CreateWindow( lpszAppName, 
  56.                         lpszTitle,    
  57.                         WS_OVERLAPPEDWINDOW, 
  58.                         CW_USEDEFAULT, 0, 
  59.                         CW_USEDEFAULT, 0,  
  60.                         NULL,              
  61.                         NULL,              
  62.                         hInstance,         
  63.                         NULL               
  64.                       );
  65.  
  66.    if ( !hWnd ) 
  67.       return( FALSE );
  68.  
  69.    ShowWindow( hWnd, nCmdShow ); 
  70.    UpdateWindow( hWnd );         
  71.  
  72.    while( GetMessage( &msg, NULL, 0, 0) )   
  73.    {
  74.       TranslateMessage( &msg ); 
  75.       DispatchMessage( &msg );  
  76.    }
  77.  
  78.    return( msg.wParam ); 
  79. }
  80.  
  81.  
  82. BOOL RegisterWin95( CONST WNDCLASS* lpwc )
  83. {
  84.    WNDCLASSEX wcex;
  85.  
  86.    wcex.style         = lpwc->style;
  87.    wcex.lpfnWndProc   = lpwc->lpfnWndProc;
  88.    wcex.cbClsExtra    = lpwc->cbClsExtra;
  89.    wcex.cbWndExtra    = lpwc->cbWndExtra;
  90.    wcex.hInstance     = lpwc->hInstance;
  91.    wcex.hIcon         = lpwc->hIcon;
  92.    wcex.hCursor       = lpwc->hCursor;
  93.    wcex.hbrBackground = lpwc->hbrBackground;
  94.    wcex.lpszMenuName  = lpwc->lpszMenuName;
  95.    wcex.lpszClassName = lpwc->lpszClassName;
  96.  
  97.    // Added elements for Windows 95.
  98.    //...............................
  99.    wcex.cbSize = sizeof(WNDCLASSEX);
  100.    wcex.hIconSm = LoadImage(wcex.hInstance, lpwc->lpszClassName, 
  101.                             IMAGE_ICON, 16, 16,
  102.                             LR_DEFAULTCOLOR );
  103.             
  104.    return RegisterClassEx( &wcex );
  105. }
  106.  
  107. LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  108. {
  109. static TOOLINFO ti;
  110. static int      index;
  111. static HWND     hToolTip = NULL;
  112.  
  113.    switch( uMsg )
  114.    {
  115.       case WM_CREATE :
  116.               {
  117.                  InitCommonControls();
  118.  
  119.                  // Create tool tip control.
  120.                  //.........................
  121.                  hToolTip = CreateWindowEx( 0,
  122.                                             TOOLTIPS_CLASS, NULL, 
  123.                                             TTS_ALWAYSTIP,
  124.                                             CW_USEDEFAULT, 
  125.                                             CW_USEDEFAULT, 
  126.                                             CW_USEDEFAULT, 
  127.                                             CW_USEDEFAULT,
  128.                                             hWnd, NULL, 
  129.                                             hInst, NULL );
  130.  
  131.                  // Add the tools to the tool tip control.
  132.                  //.......................................
  133.                  ti.cbSize = sizeof( TOOLINFO );
  134.                  ti.hwnd   = hWnd;
  135.                  ti.hinst  = hInst;
  136.                  ti.uFlags = TTF_SUBCLASS;
  137.  
  138.                  for( index=0; index < 4; index++ )
  139.                  {
  140.                     ti.uId         = index+1;
  141.                     ti.lpszText    = (LPTSTR)index+1;
  142.                     ti.rect.top    = 0;
  143.                     ti.rect.bottom = 10;
  144.                     ti.rect.left   = 10*index;
  145.                     ti.rect.right  = 10*(index+1);
  146.  
  147.                     SendMessage( hToolTip, TTM_ADDTOOL, 0, (LPARAM)&ti );
  148.                  }
  149.               }
  150.               break;
  151.  
  152.       case WM_SIZE :
  153.               {
  154.                  // Adjust the tool rectangles for the new
  155.                  // section size.
  156.                  //.......................................
  157.                  RECT     rect;
  158.  
  159.                  index = 0;
  160.  
  161.                  ti.cbSize = sizeof( TOOLINFO );
  162.  
  163.                  GetClientRect( hWnd, &rect );
  164.  
  165.                  while ( SendMessage( hToolTip, TTM_ENUMTOOLS, 
  166.                          index, (LPARAM)&ti ) )
  167.                  {
  168.                     ti.rect.top    = 0;
  169.                     ti.rect.bottom = HIWORD( lParam );
  170.                     ti.rect.left   = (rect.right/4)*index;
  171.                     ti.rect.right  = (rect.right/4)*(index+1);
  172.  
  173.                     SendMessage( hToolTip, TTM_SETTOOLINFO, index, (LPARAM)&ti );
  174.  
  175.                     index++;
  176.                  }
  177.               }
  178.               break;
  179.  
  180.       case WM_PAINT  :
  181.               {
  182.                  // Paint the vertical lines to denote the sections.
  183.                  //.................................................
  184.                  PAINTSTRUCT ps;
  185.  
  186.                  index = 0;
  187.  
  188.                  BeginPaint( hWnd, &ps );
  189.  
  190.                  while ( SendMessage( hToolTip, TTM_ENUMTOOLS, 
  191.                          index, (LPARAM)&ti ) )
  192.                  {
  193.                     MoveToEx( ps.hdc, ti.rect.right, ti.rect.top, NULL );
  194.                     LineTo( ps.hdc, ti.rect.right, ti.rect.bottom );
  195.                     index++;
  196.                  }
  197.  
  198.                  EndPaint( hWnd, &ps );
  199.               }
  200.               break;
  201.  
  202.       case WM_RBUTTONDOWN :
  203.               {
  204.                  TTHITTESTINFO tthit;
  205.  
  206.                  tthit.hwnd = hWnd;
  207.                  tthit.pt.x = LOWORD( lParam );
  208.                  tthit.pt.y = HIWORD( lParam );
  209.  
  210.                  // Check what section was hit.
  211.                  //............................
  212.                  if ( SendMessage( hToolTip, TTM_HITTEST, 0, (LPARAM)&tthit ) )
  213.                  {
  214.                     char szMsg[64];
  215.  
  216.                     // Display a message about the section hit.
  217.                     //.........................................
  218.                     wsprintf( szMsg, "Section %d was hit.", 
  219.                               tthit.ti.uId );
  220.                     MessageBox( hWnd, szMsg, lpszTitle, 
  221.                                 MB_OK | MB_ICONINFORMATION ); 
  222.                  }   
  223.               }
  224.               break;
  225.  
  226.       case WM_COMMAND :
  227.               switch( LOWORD( wParam ) )
  228.               {
  229.                  case IDM_ABOUT :
  230.                         DialogBox( hInst, "AboutBox", hWnd, (DLGPROC)About );
  231.                         break;
  232.  
  233.                  case IDM_EXIT :
  234.                         DestroyWindow( hWnd );
  235.                         break;
  236.               }
  237.               break;
  238.       
  239.       case WM_DESTROY :
  240.               PostQuitMessage(0);
  241.               break;
  242.  
  243.       default :
  244.             return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
  245.    }
  246.  
  247.    return( 0L );
  248. }
  249.  
  250.  
  251. LRESULT CALLBACK About( HWND hDlg,           
  252.                         UINT message,        
  253.                         WPARAM wParam,       
  254.                         LPARAM lParam)
  255. {
  256.    switch (message) 
  257.    {
  258.        case WM_INITDIALOG: 
  259.                return (TRUE);
  260.  
  261.        case WM_COMMAND:                              
  262.                if (   LOWORD(wParam) == IDOK         
  263.                    || LOWORD(wParam) == IDCANCEL)    
  264.                {
  265.                        EndDialog(hDlg, TRUE);        
  266.                        return (TRUE);
  267.                }
  268.                break;
  269.    }
  270.  
  271.    return (FALSE); 
  272. }
  273.